Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

506
Visualizações
Getting "Fatal error in , line 0 # Fatal JavaScript invalid size error 178414678" for a nested for loop

So I have this simple Javascript code where I am comparing database-stored cart items with client-sent new cart items. But I am getting this new error which I have never seen before:

#
# Fatal error in , line 0
# Fatal JavaScript invalid size error 178414678
#
#
#
#FailureMessage Object: 000000DCF17BE620
 1: 00007FF7F50A401F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+112511
 2: 00007FF7F4FC116F v8::CFunctionInfo::HasOptions+7055
 3: 00007FF7F5C97302 V8_Fatal+162
 4: 00007FF7F5820C65 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArray+101
 5: 00007FF7F56CA463 v8::internal::FeedbackNexus::ic_state+62771
 6: 00007FF7F56E0FC0 v8::Message::GetIsolate+15840
 7: 00007FF7F5555681 v8::internal::CompilationCache::IsEnabledScriptAndEval+26849
 8: 00007FF7F59F34B1 v8::internal::SetupIsolateDelegate::SetupHeap+494417
 9: 000001F9C44485C2

What the code does is, it checks if the product id is same or not in both the arrays. If it is then it will just replace the database cart item unit with client-sent cart item unit. If it is not, then it will just push the client-sent cart item to database-stored cart items array. That's all it does.

The code:

const dbCartItems = [
    { productID: '1233433', unit: 1 },
    { productID: 'asfa34wr', unit: 2 }
];

const clientCartItems = [
    { productID: 'dfhgdf46t3', unit: 4 },
    { productID: 'hgfh566', unit: 1 },
    { productID: '32523', unit: 1 }
];

for ( let i = 0; i < dbCartItems.length; i++ ) {

    for ( let j = 0; j < clientCartItems.length; j++ ) {

        if ( dbCartItems[ i ].productID === clientCartItems[ j ].productID ) {
            dbCartItems[ i ].unit = clientCartItems[ j ].unit;
        } else {
            dbCartItems.push( clientCartItems[ j ] );
        }

    }

}

console.log( dbCartItems );

Can someone please explain what is wrong with the code that it throws this kind of error which I have never seen before?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are continously adding to the same array which is resulting to an infinite loop. Make a new array and add items to it.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your complete logic is wrong. You are using nested loops and that would fetch wrong results even if you use a new array to store items in it.

Your code should look like this:

// get all product ids from dbCartItems array
const dbCartItemsProductIds = dbCartItems.map(cartItem => cartItem.productID);

for(let i=0;i<clientCartItems.length;i++) {
    const indexOfClientItemInDb = dbCartItemsProductIds.indexOf(clientCartItems[i].productID);
    // if clientCartItemProductId found in dbCartItemsProductIds
    if(indexOfClientItemInDb!== -1) {
        dbCartItems[indexOfClientItemInDb].unit = clientCartItems[i].unit;
    } else {
        dbCartItems.push(clientCartItems[i]);
    }
}

First, get all the productIds from dbCartItems (to match the productids from clientCartItems. Now, for each clientCartItems, check if the item product id is present in dbCartItemsProductIds, then change number of units, else add the item to dbCartItems.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda